#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

START=90
STOP=10

USE_PROCD=1
PROG=/usr/lib/sysstat/sadc
SYSSTAT_CFG="/etc/sysstat/sysstat"

validate_sysstat_section() {
	uci_load_validate sysstat sysstat "$1" "$2" \
		'log_history:uinteger' \
		'compressafter:uinteger' \
		'sadc_options:string' \
		'sa_dir:string' \
		'zip:string' \
		'enabled:string'
}

start_sysstat_instance() {
	[ "$2" = 0 ] || {
		echo "validation failed"
		return 1
	}

	[ -d /var/log/sysstat ] || mkdir -p /var/log/sysstat

	{
		echo "HISTORY=$log_history"
		echo "COMPRESSAFTER=$compressafter"
		echo "SADC_OPTIONS=\"$sadc_options\""
		echo "SA_DIR=\"$sa_dir\""
		echo "ZIP=\"$zip\""
		echo "ENABLED=\"$enabled\""
	} > $SYSSTAT_CFG

	procd_open_instance
	procd_set_param command $PROG -S DISK -F -L -
	procd_set_param file $SYSSTAT_CFG
	procd_close_instance
}

start_service() {
	validate_sysstat_section sysstat start_sysstat_instance
}

service_triggers()
{
	procd_add_reload_trigger "sysstat"
	procd_add_validation validate_sysstat_section
}
